using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class TaskParamEditForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="param"></param>
	internal delegate void SaveTaskParamHandler(TaskParam param);

	/// <summary>
	/// 当前操作的站点
	/// </summary>
	private readonly Station STATION;

	/// <summary>
	/// 实例化的目的
	/// </summary>
	internal readonly EnumFormPurpose PURPOSE;

	/// <summary>
	/// 当次编辑的触发条件
	/// </summary>
	internal readonly TaskParam PARAM;

	private const string TITLE_ADD = "添加参数";

	private const string TITLE_EDIT = "编辑参数";

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private Label label1;

	private ComboBox cmbDAQItems;

	private Button btnConfirm;

	private Button btnCancel;

	private CheckBox cbIsConst;

	private TextBox txtConst;

	private TextBox txtDefaultValue;

	private CheckBox cbSingleRead;

	/// <summary>
	///
	/// </summary>
	internal event SaveTaskParamHandler SaveTaskParam;

	/// <summary>
	///
	/// </summary>
	/// <param name="station"></param>
	/// <param name="purpose"></param>
	/// <param name="param"></param>
	internal TaskParamEditForm(Station station, EnumFormPurpose purpose, TaskParam param = null)
	{
		InitializeComponent();
		InitDAQItems(station.Id);
		STATION = station;
		PURPOSE = purpose;
		PARAM = param;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			Text = "添加参数";
			break;
		case EnumFormPurpose.Edit:
			Text = "编辑参数";
			cmbDAQItems.SelectedValue = PARAM.DAQItemId;
			cbIsConst.Checked = PARAM.IsConst;
			txtConst.Text = PARAM.Const;
			cbSingleRead.Checked = PARAM.SingleRead;
			txtDefaultValue.Text = PARAM.DefaultValue;
			break;
		}
	}

	private void TaskParamEditForm_Load(object sender, EventArgs e)
	{
		cmbDAQItems.Select();
	}

	/// <summary>
	/// 加载采集项
	/// </summary>
	private void InitDAQItems(Guid stationId)
	{
		List<DAQItem> dAQItems = Common.GetDAQItems(stationId);
		cmbDAQItems.DisplayMember = "ShowName";
		cmbDAQItems.ValueMember = "Id";
		cmbDAQItems.DataSource = dAQItems;
	}

	private void cbIsConst_CheckedChanged(object sender, EventArgs e)
	{
		txtConst.Enabled = cbIsConst.Checked;
		if (cbIsConst.Checked)
		{
			cbSingleRead.Checked = false;
		}
	}

	private void cbSingleRead_CheckedChanged(object sender, EventArgs e)
	{
		txtDefaultValue.Enabled = cbSingleRead.Checked;
		if (cbSingleRead.Checked)
		{
			cbIsConst.Checked = false;
		}
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		Guid guid = new Guid(cmbDAQItems.SelectedValue.ToString());
		TaskParam taskParam = null;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			taskParam = new TaskParam
			{
				Id = Guid.NewGuid(),
				StationId = STATION.Id,
				DAQItemId = guid,
				DAQItem = Common.GetDAQItem(STATION.Id, guid),
				IsConst = cbIsConst.Checked,
				Const = txtConst.Text.Trim(),
				SingleRead = cbSingleRead.Checked,
				DefaultValue = txtDefaultValue.Text.Trim()
			};
			break;
		case EnumFormPurpose.Edit:
			taskParam = PARAM;
			taskParam.DAQItemId = guid;
			taskParam.DAQItem = Common.GetDAQItem(STATION.Id, guid);
			taskParam.IsConst = cbIsConst.Checked;
			taskParam.Const = txtConst.Text.Trim();
			taskParam.SingleRead = cbSingleRead.Checked;
			taskParam.DefaultValue = txtDefaultValue.Text.Trim();
			break;
		}
		this.SaveTaskParam(taskParam);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void TaskParamEditForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.label1 = new System.Windows.Forms.Label();
		this.cmbDAQItems = new System.Windows.Forms.ComboBox();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.cbIsConst = new System.Windows.Forms.CheckBox();
		this.txtConst = new System.Windows.Forms.TextBox();
		this.txtDefaultValue = new System.Windows.Forms.TextBox();
		this.cbSingleRead = new System.Windows.Forms.CheckBox();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(27, 27);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(53, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "采集项：";
		this.cmbDAQItems.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbDAQItems.FormattingEnabled = true;
		this.cmbDAQItems.Location = new System.Drawing.Point(86, 24);
		this.cmbDAQItems.Name = "cmbDAQItems";
		this.cmbDAQItems.Size = new System.Drawing.Size(300, 20);
		this.cmbDAQItems.TabIndex = 0;
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(311, 156);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 1;
		this.btnConfirm.Text = "确认(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(392, 156);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 2;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.cbIsConst.AutoSize = true;
		this.cbIsConst.Location = new System.Drawing.Point(86, 52);
		this.cbIsConst.Name = "cbIsConst";
		this.cbIsConst.Size = new System.Drawing.Size(48, 16);
		this.cbIsConst.TabIndex = 8;
		this.cbIsConst.Text = "常量";
		this.cbIsConst.UseVisualStyleBackColor = true;
		this.cbIsConst.CheckedChanged += new System.EventHandler(cbIsConst_CheckedChanged);
		this.txtConst.Enabled = false;
		this.txtConst.Location = new System.Drawing.Point(140, 50);
		this.txtConst.Name = "txtConst";
		this.txtConst.Size = new System.Drawing.Size(246, 21);
		this.txtConst.TabIndex = 10;
		this.txtDefaultValue.Enabled = false;
		this.txtDefaultValue.Location = new System.Drawing.Point(236, 77);
		this.txtDefaultValue.Name = "txtDefaultValue";
		this.txtDefaultValue.Size = new System.Drawing.Size(150, 21);
		this.txtDefaultValue.TabIndex = 12;
		this.cbSingleRead.AutoSize = true;
		this.cbSingleRead.Location = new System.Drawing.Point(86, 79);
		this.cbSingleRead.Name = "cbSingleRead";
		this.cbSingleRead.Size = new System.Drawing.Size(144, 16);
		this.cbSingleRead.TabIndex = 11;
		this.cbSingleRead.Text = "读取失败时使用默认值";
		this.cbSingleRead.UseVisualStyleBackColor = true;
		this.cbSingleRead.CheckedChanged += new System.EventHandler(cbSingleRead_CheckedChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(479, 191);
		base.Controls.Add(this.txtDefaultValue);
		base.Controls.Add(this.cbSingleRead);
		base.Controls.Add(this.txtConst);
		base.Controls.Add(this.cbIsConst);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.cmbDAQItems);
		base.Controls.Add(this.label1);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "TaskParamEditForm";
		this.Text = "TaskParamEditForm";
		base.Load += new System.EventHandler(TaskParamEditForm_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(TaskParamEditForm_KeyDown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
